/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package user;

import database.Database;
import database.User;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 *
 * @author Jack
 */
public class UserInfoPanel extends javax.swing.JPanel implements ActionListener {
    User user;
    /**
     * Creates new form UserInfoPanel
     */
    public UserInfoPanel() {
        initComponents();
        //adding action listener to edit and save button
        btnEdit.addActionListener(this);
        btnSave.addActionListener(this);

        //hide textfields for editing web adress and email
        btnSave.setEnabled(false);
        tfEmail.setVisible(false);
        tfWeb.setVisible(false);
    }
    public void setUser(User user){
        this.user=user;
        lblName.setText(user.getName());
        lblAdress.setText(user.getAdress());
        lblPhone.setText(user.getPhone());
        lblEmail.setText(user.getEmail());
        lblWeb.setText(user.getWeb());
        
        listBills.setModel(new UserInfoPanelBillPopulate(user.getBills()));
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        //when button edit clicked show text fields for insert of web adress and email and enable button save
        if (e.getSource() == btnEdit) {
            editClicked();
        }
        if (e.getSource() == btnSave) {
            saveClicked();
        }
    }

    private void editClicked() {
        //getting text from labels and setting to textfields (enable/disable)
        tfEmail.setText(lblEmail.getText());
        tfWeb.setText(lblWeb.getText());
        lblEmail.setVisible(false);
        lblWeb.setVisible(false);
        tfEmail.setVisible(true);
        tfWeb.setVisible(true);
        
        btnSave.setEnabled(true);
        btnEdit.setEnabled(false);
    }

    private void saveClicked() {
        //get values from text fields and hide them
        String email = tfEmail.getText();
        String web = tfWeb.getText();
        tfEmail.setVisible(false);
        tfWeb.setVisible(false);
        
        //set text to labels and insert into database
        lblEmail.setText(email);
        lblWeb.setText(web);
        lblEmail.setVisible(true);
        lblWeb.setVisible(true);
        Database.connect();
        Database.updateEmailAndWebAddress(email, web,user.getId());
        Database.shutdown();
        
        //diable save enable edit
        btnSave.setEnabled(false);
        btnEdit.setEnabled(true);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTextField1 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        lblName = new javax.swing.JLabel();
        lblAdress = new javax.swing.JLabel();
        lblPhone = new javax.swing.JLabel();
        lblEmail = new javax.swing.JLabel();
        lblWeb = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        listBills = new javax.swing.JList();
        jLabel6 = new javax.swing.JLabel();
        btnEdit = new javax.swing.JButton();
        btnSave = new javax.swing.JButton();
        tfEmail = new javax.swing.JTextField();
        tfWeb = new javax.swing.JTextField();

        jTextField1.setText("jTextField1");

        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setText("Name:");
        add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 11, 50, -1));

        jLabel2.setText("Address:");
        add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 31, 50, -1));

        jLabel3.setText("Phone:");
        add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 51, 50, -1));

        jLabel4.setText("E-mail:");
        add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 71, 50, -1));

        jLabel5.setText("Web:");
        add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 100, 50, -1));

        lblName.setText("jLabel6");
        add(lblName, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 10, 149, -1));

        lblAdress.setText("adawd");
        add(lblAdress, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 30, 149, -1));

        lblPhone.setText("adawd");
        add(lblPhone, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 50, 149, -1));

        lblEmail.setText("adawd");
        add(lblEmail, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 70, 149, -1));

        lblWeb.setText("adawd");
        add(lblWeb, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 100, 149, -1));

        listBills.setBorder(new javax.swing.border.MatteBorder(null));
        jScrollPane1.setViewportView(listBills);

        add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(32, 162, 192, 113));

        jLabel6.setText("Bills");
        add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(32, 142, 40, -1));

        btnEdit.setText("Edit");
        add(btnEdit, new org.netbeans.lib.awtextra.AbsoluteConstraints(222, 11, 57, -1));

        btnSave.setText("Save");
        add(btnSave, new org.netbeans.lib.awtextra.AbsoluteConstraints(222, 40, -1, -1));
        add(tfEmail, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 70, 149, -1));
        add(tfWeb, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 100, 149, -1));
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    public javax.swing.JButton btnEdit;
    public javax.swing.JButton btnSave;
    public javax.swing.JLabel jLabel1;
    public javax.swing.JLabel jLabel2;
    public javax.swing.JLabel jLabel3;
    public javax.swing.JLabel jLabel4;
    public javax.swing.JLabel jLabel5;
    public javax.swing.JLabel jLabel6;
    public javax.swing.JScrollPane jScrollPane1;
    public javax.swing.JTextField jTextField1;
    public javax.swing.JLabel lblAdress;
    public javax.swing.JLabel lblEmail;
    public javax.swing.JLabel lblName;
    public javax.swing.JLabel lblPhone;
    public javax.swing.JLabel lblWeb;
    public javax.swing.JList listBills;
    public javax.swing.JTextField tfEmail;
    public javax.swing.JTextField tfWeb;
    // End of variables declaration//GEN-END:variables
}
